package com.mop.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mop.app.controller.vo.CollectSourceVO;
import com.mop.app.controller.vo.UserCollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import com.mop.app.dal.entity.AppUser;
import com.mop.app.common.PageQueryWrapper;
import org.springframework.web.bind.annotation.RestController;
import com.mop.app.common.BaseController;
import com.mop.app.service.AppUserService;

import java.util.List;
/**
 * 用户管理
 *
 * @author wchen
 * @since 2020-02-27
 */
@RestController
@RequestMapping("/open/appUser")
public class UserController extends BaseController {

    @Autowired
    private AppUserService appUserService;


    /**
     * 分页查询
     *
     * @param query 默认使用实体作为查询参数，可根据业务需要替换为查询对象，如若替换为查询对象，请修改相应mapper文件的sql查询
     * @return 分页结果集
     */
    @PostMapping("/list")
    public Page<AppUser> list(@RequestBody PageQueryWrapper<AppUser> query) {

        query.setQueryClazz(AppUser.class);
        Page pageList = appUserService.pageQuery(query);

        return pageList;
    }


    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    @PostMapping("/remove")
    public void remove(@RequestBody List<Long> idList) throws Exception {
        if (appUserService.removeByIds(idList)) {
            return;
        }
        throw new Exception();
    }

    /**
     * 用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AppUser getById(@PathVariable Long id) {
        AppUser appUser = appUserService.getById(id);
        return appUser;
    }


    /**
     * 退出
     *
     * @return
     */
    @GetMapping("/logout")
    public void logout() {
        appUserService.logout(getToken());
    }


    /**
     * 用户收藏
     *
     * @return
     */
    @GetMapping("/collectList")
    public Page<CollectSourceVO> userCollect() {
        List<CollectSourceVO> sourceVOS = appUserService.userCollect(getUserId());
        Page page = new Page();
        page.setRecords(sourceVOS);
        page.setTotal(sourceVOS.size());
        return page;
    }

    /**
     * 添加用户收藏
     *
     * @return
     */
    @PostMapping("/addCollect")
    public void addCollect(@RequestBody UserCollectVO userCollectVO) {
        appUserService.addCollect(getUserId(), userCollectVO.getSourceId());
    }
}

